package com.sum.tea.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.sum.common.bean.ReaderExt.READER_ERR
import com.sum.common.bean.ReaderExt.Region_Conf
import com.sum.common.bean.ReaderExt.SL_TagProtocol
import com.sum.common.bean.SessionInfo
import com.sum.common.manager.UHFManager
import com.sum.common.utils.LogU
import com.sum.tea.R
import com.sum.tea.constant.Constants

class SettingViewModel : ViewModel() {
    private val _sessionInfo = MutableLiveData<SessionInfo>()
    val sessionInfo: LiveData<SessionInfo> = _sessionInfo

    // Region operations
    fun getRegion():String {
        val rcf = UHFManager.getInstance().currentManager.getRegion()
        val regionValue = getRegionValue(rcf)
        val regionNames = Utils.getApp().resources.getStringArray(R.array.spireg)
        return if (regionValue < regionNames.size) regionNames[regionValue] else "Unknown"
    }

    fun setRegion(selectedRegion: Int) {
        LogU.i( "selectedRegion: $selectedRegion")
        val result = UHFManager.getInstance().currentManager.setRegion(selectedRegion)
        if (result == READER_ERR.MT_OK_ERR) {
            ToastUtils.showLong("setRegion success")
        }else{
            ToastUtils.showLong("setRegion fail:$result")
        }
    }

    // Power operations
    fun getPower():Int {
        val result = UHFManager.getInstance().currentManager.getPower()
        if (result !=null) {
            ToastUtils.showLong("Current Power: ${result[0]+1}")
            return result[0]
        }
        return -1
    }

    fun setPower(selectedPower: Int) {
        LogU.i( "selectedPower: $selectedPower")
        val result = UHFManager.getInstance().currentManager.setPower(selectedPower)
        if (result == READER_ERR.MT_OK_ERR) {
            SPUtils.getInstance().put(Constants.KEY_POWER, selectedPower)
            ToastUtils.showLong("Current Power: ${selectedPower+1}")
        }else{
            ToastUtils.showLong("Current Power: $result")
        }
    }

    // Protocol operations
    fun getProtocol():String {
        val protocol = UHFManager.getInstance().currentManager.getProtocol()
        return when(protocol){
            SL_TagProtocol.SL_TAG_PROTOCOL_IPX64.value() -> "IPX64"
            SL_TagProtocol.SL_TAG_PROTOCOL_GEN2.value() -> "Gen2"
            SL_TagProtocol.SL_TAG_PROTOCOL_ISO180006B_UCODE.value(),SL_TagProtocol.SL_TAG_PROTOCOL_ISO180006B.value()  -> "6B"
            SL_TagProtocol.SL_TAG_PROTOCOL_IPX256.value() -> "IPX256"
            else -> "Gen2"
        }
    }

    fun setProtocol(selectedProtocol: String) {
        val result = UHFManager.getInstance().currentManager.setProtocol(selectedProtocol)
        if (result == READER_ERR.MT_OK_ERR) {
            ToastUtils.showLong("setProtocol success")
        }
    }

    // Q Value operations
    fun getQValue():Int {
        val qValue = UHFManager.getInstance().currentManager.getQValue()
        ToastUtils.showLong("Current qValue: $qValue")
        return qValue
    }

    fun setQValue(selectedPosition: Int) {
        val result = UHFManager.getInstance().currentManager.setQValue(selectedPosition)
        if (result == READER_ERR.MT_OK_ERR) {
            ToastUtils.showLong("setQValue success")
        }
    }

    fun getSessionAndFlag() {
        _sessionInfo.value = SessionInfo(
            UHFManager.getInstance().currentManager.getSession(),
            UHFManager.getInstance().currentManager.getFlag(),
        )
    }

    fun getRegionValue(regionConf: Region_Conf): Int = when (regionConf) {
    Region_Conf.RG_PRC -> 0
    Region_Conf.RG_PRC2 -> 1
    Region_Conf.RG_NONE -> 2
    Region_Conf.RG_NA -> 3
    Region_Conf.RG_EU3 -> 4
    else -> 8
}

}